package com.example.demo.httpclient;

import cn.hutool.core.date.DateUtil;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liujiahao on 2019/1/25.
 */
public class HttpClient {
    public static void main(String[] args) {
        try {
            RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
            CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
            CloseableHttpResponse response=null;
            String content="";
            List<NameValuePair> nvps=new ArrayList<>();
            String url="http://172.16.25.189:8888/group1/M00/00/1B/rBAZvl0e0pqAGM1CAA1rIoHwd3k126.jpg";
            url="https://scontent.xx.fbcdn.net/v/t45.1600-4/s128x128/21560903_6073955619701_3839992807515226112_n.png?_nc_cat=107&_nc_oc=AQmuknfTT1YACJ7qHxq8uUFUQbSNkJ_oFwmLJ-KG2cD72xcl266FJfejymYFQLzzj58&_nc_ht=scontent.xx&oh=89c4a92ac1a9a8f2f17d6df6bd964482&oe=5DEC50E5";
            nvps.add(new BasicNameValuePair("url",url));
            HttpPost httpPost = new HttpPost("http://172.16.25.163:8080/api/file/copy");
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
            // setConnectTimeout：设置连接超时时间，单位毫秒。
            // setConnectionRequestTimeout：设置从connect Manager获取Connection
            // 超时时间，单位毫秒。这个属性是新加的属性，因为目前版本是可以共享连接池的。
            // setSocketTimeout：请求获取数据的超时时间，单位毫秒。
            // 如果访问一个接口，多少时间内无法返回数据，就直接放弃此次调用。
            RequestConfig requestConfigs = RequestConfig.custom().setConnectTimeout(150000)
                    .setConnectionRequestTimeout(150000).setSocketTimeout(150000).build();
            httpPost.setConfig(requestConfigs);
            response=httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            switch (statusCode) {
                case 200: {
                    // 得到相应实体、包括响应头以及相应内容
                    HttpEntity entity = response.getEntity();
                    // 得到response的内容
                    content = EntityUtils.toString(entity);
                    // 关闭输入流
                    EntityUtils.consume(entity);
                }
                break;
                case 302:
                case 301: {
                    Header[] hs = response.getHeaders("Location");
                    HttpEntity entity = response.getEntity();
                    // String entityutils=EntityUtils.toString(entity, "UTF-8");
                    content = EntityUtils.toString(entity);
                    if (hs.length > 0) {
                        String location = hs[0].toString().replaceAll("Location: ", "");
                        System.out.println(location);
                        HttpGet httpGet=new HttpGet(location);
                        response= httpClient.execute(httpGet);
                        HttpEntity entitys = response.getEntity();
                        // 得到response的内容
                        content = EntityUtils.toString(entitys);
                        // 关闭输入流
                        EntityUtils.consume(entitys);
                       /* if (location.substring(0, 1).equals("/")) {
                            location = this.properties.get(Fetcher_Login_Url).toString() + location;
                        }
                        content = sendGet(headers, location);*/

                    }
                }

                break;
                default:
                    break;
            }
            /*HttpGet httpGet=new HttpGet("http://192.168.10.12:8080/dingdan/list");
            response= httpClient.execute(httpGet);
            HttpEntity entitys = response.getEntity();
            // 得到response的内容
            content = EntityUtils.toString(entitys);
            // 关闭输入流
            EntityUtils.consume(entitys);*/
        }catch (Exception e){

        }finally {

        }

    }
}
